package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.vip.qiyu.constant.CacheConstants;
import net.qhzw.vip.qiyu.entity.SysUserTopupOrder;
import net.qhzw.vip.qiyu.model.SysUserTopupOrderVo;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.result.PageResult;
import net.qhzw.vip.qiyu.service.SysUserTopupOrderService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Map;

@RestController
@RequestMapping("/sys/user/topupOrder")
@Api(tags = "用户充值订单表管理")
@Slf4j
public class SysUserTopupOrderController {

    @Autowired
    private SysUserTopupOrderService dtsUserTopupOrderService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "分页查询用户充值订单")
    @GetMapping
    public JsonResult<PageResult<SysUserTopupOrderVo>> findByPage(Page<SysUserTopupOrder> page, SysUserTopupOrderVo userTopupOrderVo) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        PageResult<SysUserTopupOrderVo> pageResult = dtsUserTopupOrderService.findByPage(page, userTopupOrderVo, shopId);
        return JsonResult.ok(pageResult);
    }

    @ApiOperation(value = "导出充值订单")
    @PostMapping("exportLedger")
    public  void exportLedger(HttpServletResponse response, @RequestBody(required = false) SysUserTopupOrderVo userTopupOrderVo) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        dtsUserTopupOrderService.exportLedger(response, shopId, userTopupOrderVo);
    }

    @ApiOperation(value = "用户充值金额")
    @PostMapping("usrRchgGold")
    public JsonResult usrRchgGold(@RequestBody Map<String, String> param) {
        BigDecimal amount = new BigDecimal(param.get("amount"));
        Long userId = SecurityUtils.getUserId();
        WxPayMpOrderResult wxPayUnifiedOrderRequest = dtsUserTopupOrderService.usrRchgGold(amount, userId);
        return JsonResult.ok(wxPayUnifiedOrderRequest);
    }


    @ApiOperation(value = "用户充值支付回调")
    @PostMapping("pay/notify")
    public JsonResult payNotify(@RequestBody String xmlData) {
        //log.info("【用户充值支付回调】： {}", xmlData);
        WxPayOrderNotifyResult rs = WxPayOrderNotifyResult.fromXML(xmlData);
        String SubMchId = rs.getSubMchId();
        String outTradeNo = rs.getOutTradeNo();
        String transactionId = rs.getTransactionId();
        WxPayConfig wxPayConfig = new WxPayConfig();
        wxPayConfig.setSubMchId(SubMchId);
        Object orderCache = redisTemplate.boundListOps(CacheConstants.USER_ORDER_CACHE + ":" + outTradeNo).rightPop();
        if (orderCache != null){
            dtsUserTopupOrderService.payNotify(outTradeNo, transactionId);
        }
        return JsonResult.ok();
    }

    @ApiOperation(value = "后台审核充值")
    @PutMapping("audit")
    public JsonResult audit(@RequestBody Map<String, String> param) {
        Long id = Long.valueOf(param.get("id"));
        Byte status = Byte.valueOf(param.get("status"));
        String auditOpinion = param.get("auditOpinion");
        Long userId = Long.valueOf(param.get("userId"));
        boolean isOK = dtsUserTopupOrderService.audit(id, status, auditOpinion, userId);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


}
